package com.settlement.system.controller;

import com.settlement.system.common.exception.ServiceException;
import com.settlement.system.reqparam.ReceiveRefundRateReqParam;
import com.settlement.system.service.IBaseService;
import com.settlement.system.service.ITblUnificationBillDetailService;
import com.settlement.system.common.util.common.FasUtil;
import com.alibaba.excel.context.AnalysisContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import com.settlement.system.model.entity.common.TblReceiveRefundRate;
import jakarta.annotation.Resource;
import com.settlement.system.service.ITblReceiveRefundRateService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 收退款-比率表 前端控制器
 * @author Ximi
 * @since 2020-06-12
 */

@Api(tags = "基础配置-比率表",description = "基础配置-比率表")
@RestController
@RequestMapping("/receive_refund/rate")
@Slf4j
public class TblReceiveRefundRateController extends MergeApiExcelController<TblReceiveRefundRate, ReceiveRefundRateReqParam> {

    @Resource
    private ITblReceiveRefundRateService tblReceiveRefundRateService;

    @Resource
    private ITblUnificationBillDetailService tblUnificationBillDetailService;

    @Override
    public IBaseService init() {
        return tblReceiveRefundRateService;
    }

    @Override
    public Class<?> getModelClass() {
        return TblReceiveRefundRate.class;
    }

    @Override
    protected void setParamsModel(Map<String, Object> model) {
        FasUtil.formatInQueryCondition(model,"shopNo");
        super.setParamsModel(model);
    }

    @Override
    public String verifyImportObject(TblReceiveRefundRate tblReceiveRefundRate, AnalysisContext analysisContext) {

        if(StringUtils.isEmpty(tblReceiveRefundRate.getActionId())){
            return "活动ID不能为空";
        }

        if(StringUtils.isEmpty(tblReceiveRefundRate.getBillType())){
            return "类型不能为空";
        }

        if(tblReceiveRefundRate.getExtractMoneyDate() == null &&
                StringUtils.isEmpty(tblReceiveRefundRate.getPermitDifferentComfire())){
            return "'提现日期(返款日期)'和'允许有差异确认'不能同时为空";
        }

        try {
            if(tblUnificationBillDetailService.getAkcPushedCount(tblReceiveRefundRate.getActionId(),
                    tblReceiveRefundRate.getBillType()) > 0){
                return "已经生产NC凭证明细,无法更新";
            }
        } catch (Exception ex){
            return "系统异常,请联系管理员";
        }
        return null;
    }

    @Override
    protected void saveData(List<TblReceiveRefundRate> datas) throws ServiceException {
        tblReceiveRefundRateService.batchUpdatefinanceData(datas);
        try {
            for (TblReceiveRefundRate info:datas){
                if(info.getExtractMoneyDate()!=null){
                    tblUnificationBillDetailService.updateAkcDate(info.getActionId(),info.getBillType(),info.getExtractMoneyDate());
                    tblUnificationBillDetailService.refreshNoMatchByActionIdOfAkc(info.getActionId());
                }
            }
        } catch (Exception ex){
            log.error("更新爱库存异常",ex);
        }
    }
}

